package com.zjy.controller;

import com.zjy.pojo.Students;
import com.zjy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/studentmanager")
public class StudentController {
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;

    @RequestMapping("")
    public String studentslist(Model model){
        List<Students> list = studentService.queryAllStudent();
        model.addAttribute("studentlist",list);
        return "studentlist";
    }

    //跳转到增加的页面
    @RequestMapping("/tostudentinsert")
    public String tostudentinsert(){
        return "studentinsert";
    }
    //添加书籍的请求
    @RequestMapping("/studentinsert")
    public String studentinsert(Students students){
        System.out.println("studentinsert==>"+students);
        studentService.addStudent(students);
        return "redirect:/studentmanager";
    }

    //跳转到修改的页面
    @RequestMapping("/tostudentupdate")
    public String tostudentupdate(int id,Model model){
        Students student = studentService.queryStudentById(id);
        System.out.println(student);
        model.addAttribute("student",student);
        return "studentupdate";
    }
    //修改学生的请求
    @RequestMapping("/studentupdate")
    public String studentupdate(Students students){
        System.out.println("studentupdate==>"+students);
        studentService.updateStudent(students);

        return "redirect:/studentmanager";
    }

    //跳转到删除的页面
    @RequestMapping("/tostudentshow")
    public String tostudentshow(int id,Model model){
        Students student = studentService.queryStudentById(id);
        System.out.println(student);
        model.addAttribute("student",student);
        return "studentshow";
    }
    //删除学生的请求
    @RequestMapping("/studentshow")
    public String studentshow(int id){
        System.out.println("studentshow=>>"+id);
        studentService.deleteStudentById(id);

        return "redirect:/studentmanager";
    }

}
